package cn.wolfcode.trip.admin.web.controller;


import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/strategyTravel")
@Controller
public class StrategyTravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("/list")
    public Object queryStrategyTravel(Model model,@ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo=travelService.queryStrategyTravel(qo);
        model.addAttribute("result", pageInfo);
        StrategyQueryObject strategyQueryObject = new StrategyQueryObject();
        strategyQueryObject.setState(Strategy.STATE_HOT);
        strategyQueryObject.setPageSize(0);
        model.addAttribute("strategies", strategyService.listByState(strategyQueryObject).getList());
        qo.setPageSize(0);
        qo.setIsPublic(true);
        qo.setState(Travel.STATE_RELEASE);
        model.addAttribute("travels", travelService.getPageInfo(qo).getList());
        return "strategyTravel/list";
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(Long id){
        strategyService.deleteStrategyTravel(id);
        return new JSONResult();
    }
    @ResponseBody
    @RequestMapping("/save")
    public Object saveStrategyTravel(Long strategyId,Long travelId){
        JSONResult result = new JSONResult();
        try {
            strategyService.saveStrategyTravel(strategyId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
